package com.itany.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.itany.constant.Constant;
import com.itany.constant.ResponseConstant;
import com.itany.exception.BookDisabledException;
import com.itany.exception.FileUploadErrorException;
import com.itany.exception.RequestParamsException;
import com.itany.pojo.Book;
import com.itany.pojo.BookSupplier;
import com.itany.pojo.Supplier;
import com.itany.service.BookService;
import com.itany.vo.ActionResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/book")
public class BookController {

    @Autowired
    BookService bookService;


    @RequestMapping("/getonline")
    @ResponseBody
    public ActionResult getBooksinfoOnline(String ISBN) {
        ActionResult result = new ActionResult();
        try {
            Book booksinfoOnline = bookService.getBooksinfoOnline(ISBN);
            result.setMsg("获取成功");
            result.setStatus(ResponseConstant.RESPONSE_SUCCESS);
            result.setData(booksinfoOnline);
        } catch (RequestParamsException e) {
            result.setMsg(e.getMessage());
            result.setStatus(ResponseConstant.REQUEST_PARAMETER_ERROR);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("服务器内部异常");
            result.setStatus(ResponseConstant.SERVER_ERROR);
        }
        return result;

    }


    @RequestMapping("/add")
    @ResponseBody
    public ActionResult addBook(String bookname, String ISBN, String price, String number, String groupprice, String pagenumber, String author, String publishdate, String bookintroduce, String authorintroduce,
                                @RequestParam(value = "file", required = false) MultipartFile file,
                                HttpSession session) {
        ActionResult result = new ActionResult();
        try {
            bookService.addBook(bookname, ISBN, price, number, groupprice, pagenumber, author, publishdate, bookintroduce, authorintroduce, file, session);
            result.setMsg("添加成功");
            result.setStatus(ResponseConstant.RESPONSE_SUCCESS);
        } catch (RequestParamsException e) {
            result.setMsg(e.getMessage());
            result.setStatus(ResponseConstant.REQUEST_PARAMETER_ERROR);
        } catch (BookDisabledException | FileUploadErrorException e) {
            result.setMsg(e.getMessage());
            result.setStatus(ResponseConstant.RESPONSE_FAIL);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("服务器内部异常");
            result.setStatus(ResponseConstant.SERVER_ERROR);
        }
        return result;
    }

    /**
     * 查找所有图书
     *
     * @param page 页码数
     * @param rows 每页数据条数
     * @return 分页图书信息
     */
    @RequestMapping("/findAll")
    @ResponseBody
    public Map<String, Object> findAll(@RequestParam(defaultValue = "1") Integer page,
                                       @RequestParam(defaultValue = "10") Integer rows,
                                       String booknameKey, String ISBNKey, String supplierId, String pressId) {
        Map<String, Object> map = new HashMap<>();
        PageInfo<Book> info;

        info = bookService.findAllBooks(page, rows, booknameKey, ISBNKey, supplierId, pressId);

        map.put("total", info.getTotal());
        map.put("rows", info.getList());
        return map;
    }


    /**
     * 修改图书信息
     */
    @RequestMapping("/modifyBook")
    @ResponseBody
    public ActionResult modifyBook(String bookid, String bookname, String price, String groupprice,
                                   String bookintroduce, String authorintroduce, @RequestParam(value = "file",
            required = false) MultipartFile file) {
        ActionResult result = new ActionResult();
        try {
            bookService.modifyBook(bookid, bookname, price, groupprice, bookintroduce, authorintroduce, file);
            result.setMsg("修改成功");
            result.setStatus(ResponseConstant.RESPONSE_SUCCESS);
        } catch (RequestParamsException e) {

            result.setMsg(e.getMessage());
            result.setStatus(ResponseConstant.REQUEST_PARAMETER_ERROR);
        } catch (FileUploadErrorException e) {
            result.setMsg(e.getMessage());
            result.setStatus(ResponseConstant.RESPONSE_FAIL);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("服务器内部异常");
            result.setStatus(ResponseConstant.SERVER_ERROR);
        }
        return result;
    }


    /**
     * 设置指定图书状态
     *
     * @param id     图书id
     * @param status 该图书待修改的状态
     * @return 响应数据
     */
    @RequestMapping("/status/{status}")
    @ResponseBody
    public ActionResult enable(String id, @PathVariable("status") Integer status) {
        ActionResult result = new ActionResult();
        try {
            bookService.modifyStatus(id, status);
            result.setStatus(ResponseConstant.RESPONSE_SUCCESS);
        } catch (RequestParamsException e) {
            e.printStackTrace();
            result.setMsg(e.getMessage());
            result.setStatus(ResponseConstant.REQUEST_PARAMETER_ERROR);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("服务器内部异常");
            result.setStatus(ResponseConstant.SERVER_ERROR);
        }
        return result;
    }


    /**
     * 查找当前登录供应商的图书信息
     *
     * @param page 页码数
     * @param rows 每页数据条数
     * @return 分页图书信息
     */
    @RequestMapping("/findBooksBySupplier")
    @ResponseBody
    public Map<String, Object> findBooksBySupplier(@RequestParam(defaultValue = "1") Integer page,
                                                   @RequestParam(defaultValue = "10") Integer rows,
                                                   String booknameKey, String ISBNKey, HttpSession session) {
        Map<String, Object> map = new HashMap<>();
        PageInfo<BookSupplier> info;
        info = bookService.findBooksBySupplier(page, rows, session, booknameKey, ISBNKey);
        map.put("total", info.getTotal());
        map.put("rows", info.getList());
        return map;
    }
}
